package com.dcbeida.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.activity.HomeActivity;
import com.dcbeida.activity.purchase.PurchaseExitActivity;
import com.dcbeida.activity.stockdetail.KcpdbgActivity;
import com.dcbeida.activity.stockdetail.RkStockDetailActivity;
import com.dcbeida.activity.stockdetail.StockDetailActivity;
import com.dcbeida.activity.store.StoreActivity;
import com.dcbeida.adapter.SocketAdapter;
import com.dcbeida.bean.Drug;
import com.dcbeida.bean.DrugBean;
import com.dcbeida.bean.DrugListBean;
import com.dcbeida.bean.Stock;
import com.dcbeida.fragment.stockFragment.StockContract;
import com.dcbeida.fragment.stockFragment.StockPersenter;
import com.dcbeida.widget.DataLoadDirection;
import com.dcbeida.widget.XListView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.dcbeida.utils.Utils.showTip;


/**
 * 库存
 * Created by swt on 2016/11/17.
 */

public class StockFragment extends BaseFragment implements XListView.IXListViewListener,
        StockContract.View, AdapterView.OnItemClickListener, View.OnClickListener {


    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.rel_title1)
    RelativeLayout relTitle1;
    @Bind(R.id.tv_kc)
    TextView tvKc;
    @Bind(R.id.tv_tx)
    TextView tvTx;
    @Bind(R.id.tv_yprk)
    TextView tvYprk;
    @Bind(R.id.tv_kcpd)
    TextView tvKcpd;
    @Bind(R.id.myList)
    XListView myList;
    private ArrayList<Object> datalist;
    private SocketAdapter adapter;
    private StockPersenter mPresent;

    private boolean isEdit = false;
    private boolean isEnd = false;// 是否有更多
    private int Totle = 0;//
    private int pageindex = 0;
    private int pagesize = 10;
    int type = DataLoadDirection.Refresh;

    @Override
    public View initViews() {
        View view = View.inflate(mActivity, R.layout.stock_fragment, null);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void initData() {
        datalist = new ArrayList<Object>();
        myList.setPullLoadEnable(false);
        myList.setPullRefreshEnable(true);
        myList.setXListViewListener(this);
        myList.setOnItemClickListener(this);
        myList.setVerticalScrollBarEnabled(false);// 去掉竖向滚动条
        adapter = new SocketAdapter(getActivity());
        addHeader();
        myList.setAdapter(adapter);

        mPresent = new StockPersenter(this, getActivity());
        //mPresent.getList(pageindex, pagesize);
    }

    @Override
    public void onResume() {
        super.onResume();
        if(adapter.getmArrayList().size() == 0)
            mPresent.getList(pageindex, pagesize);
        else
            mPresent.getList(pageindex, pagesize,false);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {   // 不在最前端显示 相当于调用了onPause();
        } else {  // 在最前端显示 相当于调用了onResume();
            //刷新数据
            reFresh();
        }
    }




    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.tv_right, R.id.tv_yprk, R.id.tv_kcpd})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_right:
                ((HomeActivity) getActivity()).showActivity(StockDetailActivity.class, null);
                break;
            case R.id.tv_yprk:
                ((HomeActivity) mActivity).showActivity(StoreActivity.class, null);
                break;
            case R.id.tv_kcpd:
                ((HomeActivity) mActivity).showActivity(KcpdbgActivity.class, null);
                break;

            case R.id.tv_cgtc:
                ((HomeActivity) mActivity).showActivity(PurchaseExitActivity.class, null);
                break;


        }
    }

    @Override
    public void setPresenter(StockContract.Persent presenter) {

    }

    @Override
    public void showTelTip(String msg) {
        showTip(getActivity(), msg);
    }

    @Override
    public void showView(boolean hasData) {

    }


    @Override
    public void showList(ArrayList<Stock.ListBean> list) {

        if (list.size() < 10)
            isEnd = false;
        else
            isEnd = true;

        Totle = adapter.getmArrayList().size() + list.size();


        if (type == DataLoadDirection.Refresh) {
            myList.stopRefresh();
            pageindex = 0;
            adapter.clearList();
        } else if (type == DataLoadDirection.LoadMore) {
            myList.stopLoadMore();
            pageindex++;
        }

        if (isEnd) {
            myList.showFooterView();
            myList.setPullLoadEnable(true);
        } else {
            myList.hideFooterView();
            myList.setPullLoadEnable(false);
        }

        adapter.setmArrayList(list);
    }

    @Override
    public void showData(Stock stock) {
//        tvKc.setText(stock.getStock() + "");
//        tvTx.setText(stock.getDaysforsale() + "");


        LinearLayout lin_add = (LinearLayout) mActivity.findViewById(R.id.lin_add);
        lin_add.removeAllViews();
        int pos = 0;



        String stockCount = null;
        String dsysForsale = null;
        for (DrugBean drugBean : stock.getDruglist()) {

            //todo 添加name

            stockCount = drugBean.getStock();
            dsysForsale = drugBean.getDaysforsale();
            if (pos != 0) {
                LayoutInflater.from(mActivity).inflate(R.layout.stock_detail_header_line, lin_add, true);
            }
            View itemView = LayoutInflater.from(mActivity).inflate(R.layout.stock_detail_header_item, null);
            lin_add.addView(itemView);
            TextView tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            TextView tv_count = (TextView) itemView.findViewById(R.id.tv_count);
            TextView tv_day = (TextView) itemView.findViewById(R.id.tv_day);

            tv_name.setText(drugBean.getDrugname());
            tv_count.setText(stockCount);
            tv_day.setText(dsysForsale);

            pos++;
        }

    }

    /**
     * 测试 todo
     */
    @Override
    public void addHeader() {
        View header = LayoutInflater.from(mActivity).inflate(R.layout.stock_fr_header, null);
        TextView tv_cgtc = (TextView) header.findViewById(R.id.tv_cgtc);
        TextView tv_yprk = (TextView) header.findViewById(R.id.tv_yprk);
        TextView tv_kcpd = (TextView) header.findViewById(R.id.tv_kcpd);
        tv_cgtc.setOnClickListener(this);
        tv_yprk.setOnClickListener(this);
        tv_kcpd.setOnClickListener(this);
        myList.addHeaderView(header);
    }


    @Override
    public void onRefresh() {
        // 执行刷新任务
        type = DataLoadDirection.Refresh;
        mPresent.getList(0, pagesize);
    }

    public void reFresh() {
        type = DataLoadDirection.Refresh;
        mPresent.getList(0, pagesize,false);
    }

    @Override
    public void onLoadMore() {
        // 执行加载更多任务
        type = DataLoadDirection.LoadMore;
        if (isEnd) {
            mPresent.getList(pageindex + 1, pagesize);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Stock.ListBean bean = (Stock.ListBean) adapter.getItem(position - 2);
        Bundle bundle = new Bundle();
        bundle.putSerializable("Stock.ListBean", bean);
        ((HomeActivity) mActivity).showActivity(RkStockDetailActivity.class, bundle);
    }
}
